<?php
header("Content-type:text/html;charset=utf-8");
$dsn = "mysql:host=localhost;dbname=c50";
$username="root";
$password = "admin888";
try{
	$pdo = new Pdo($dsn,$username,$password);
}catch(PDOException $e) {
	die($e->getMessage());
}

//设置错误处理方法
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//设置字符集
$pdo->query("set names utf8");

try {
	$result = $pdo->query("select * from stu");

	$data =$result->fetchAll(PDO::FETCH_OBJ);
	echo $data[0]->sname;
} catch (Exception $e) {
	echo $e->getMessage();
}

//删除
try {
    $sql = "delete from stu where id>2";
    // 返回受影响的函数
    $affected_rows = $pdo->exec($sql);
    
    echo "共删除成功{$affected_rows}条记录";
} catch (PDOException $e) {
    echo $e->getMessage();
}